From 64530b706ec476cb097f7636bf58a0c5f7b4747f Mon Sep 17 00:00:00 2001 From: Inokentiy Babushkin Date: Fri, 2 Dec 2016 00:03:49 +0100 Subject: [PATCH] Improved error handling to reflect the actual situation, added tests * One of the tests still doesn't pass, this needs further investigation --- src/cargo/ops/cargo_rustc/context.rs | 24 +++++++- tests/rustflags.rs | 86 ++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 6be2bef02..2ec275e71 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -856,19 +856,37 @@ fn env_args(config: &Config, // Then the target.*.rustflags value let target = build_config.requested_target.as_ref().unwrap_or(&build_config.host_triple); let key = format!("target.{}.{}", target, name); - if let Some(args) = config.get_list(&key)? { + let list = config.get_list(&key); + if let Ok(Some(args)) = list { let args = args.val.into_iter().map(|a| a.0); return Ok(args.collect()); } + let string = config.get_string(&key); + if let Ok(Some(arg)) = string { + return Ok(arg.val.split(' ').map(str::to_string).collect()); + } + if list.is_err() && string.is_err() { + if let Some(value) = config.values()?.get(&key) { + return config.expected("list or string", &key, value.clone()); + } + } // Then the build.rustflags value let key = format!("build.{}", name); - if let Some(args) = config.get_list(&key)? { + let list = config.get_list(&key); + if let Ok(Some(args)) = list { let args = args.val.into_iter().map(|a| a.0); return Ok(args.collect()); - } else if let Some(arg) = config.get_string(&key)? { + } + let string = config.get_string(&key); + if let Ok(Some(arg)) = string { return Ok(arg.val.split(' ').map(str::to_string).collect()); } + if list.is_err() && string.is_err() { + if let Some(value) = config.values()?.get(&key) { + return config.expected("list or string", &key, value.clone()); + } + } Ok(Vec::new()) } diff --git a/tests/rustflags.rs b/tests/rustflags.rs index cc9266ac2..1cf9e96cd 100644 --- a/tests/rustflags.rs +++ b/tests/rustflags.rs @@ -949,3 +949,89 @@ fn target_rustflags_precedence() { assert_that(p.cargo("bench"), execs().with_status(101)); } + +#[test] +fn target_rustflags_string_and_array_form1() { + let p1 = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", "") + .file(".cargo/config", r#" + [build] + rustflags = ["--cfg", "foo"] + "#); + p1.build(); + + assert_that(p1.cargo("build").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg foo[..]` +[FINISHED] debug [unoptimized + debuginfo] target(s) in [..] +")); + + let p2 = project("foo") + .file("Cargo.toml", r#" + [package] + name = "foo" + version = "0.0.1" + "#) + .file("src/lib.rs", "") + .file(".cargo/config", r#" + [build] + rustflags = "--cfg foo" + "#); + p2.build(); + + assert_that(p2.cargo("build").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg foo[..]` +[FINISHED] debug [unoptimized + debuginfo] target(s) in [..] +")); + +} + +#[test] +fn target_rustflags_string_and_array_form2() { + let p1 = project("foo") + .file("Cargo.toml", &format!(r#" + [package] + name = "foo" + version = "0.0.1" + + [target.{}] + rustflags = ["--cfg", "foo"] + "#, rustc_host())) + .file("src/lib.rs", ""); + p1.build(); + + assert_that(p1.cargo("build").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg foo[..]` +[FINISHED] debug [unoptimized + debuginfo] target(s) in [..] +")); + + let p2 = project("foo") + .file("Cargo.toml", &format!(r#" + [package] + name = "foo" + version = "0.0.1" + + [target.{}] + rustflags = "--cfg foo" + "#, rustc_host())) + .file("src/lib.rs", ""); + p2.build(); + + assert_that(p2.cargo("build").arg("-v"), + execs().with_status(0).with_stderr("\ +[COMPILING] foo v0.0.1 ([..]) +[RUNNING] `rustc [..] --cfg foo[..]` +[FINISHED] debug [unoptimized + debuginfo] target(s) in [..] +")); + +} -- 2.30.2